package bb.lanxing.adapter.discover;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import bb.lanxing.App;
import bb.lanxing.R;

public class DiscoverIconAdapter extends RecyclerView.Adapter<DiscoverIconAdapter.ViewHolder> {
    private int[] iconList = {R.drawable.ic_prefer, R.drawable.ic_rank, R.drawable.ic_order, R.drawable.ic_find_news, R.drawable.ic_lesson};
    private int[] iconNameList = {R.string.discover_1, R.string.discover_3, R.string.discover_6, R.string.discover_7, R.string.discover_4};
    private OnItemClickListener listener;
    private Context mContext;

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView name;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.itemIcon);
            name = itemView.findViewById(R.id.itemText);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_discovery_icon, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.icon.setImageResource(iconList[position]);
        holder.name.setText(App.getContext().getString(iconNameList[position]));
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return iconNameList.length;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}